package com.google.gwt.sample.dynatablerf.shared;

import com.google.web.bindery.requestfactory.gwt.client.DefaultRequestTransport;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

public class GrifoRequestTransport extends DefaultRequestTransport {

	private IGrifoTransportInterceptor grifoTransportInterceptor;

	class GrifoTransportReceiver implements TransportReceiver {

		private TransportReceiver transportReceiver;

		public GrifoTransportReceiver(TransportReceiver transportReceiver) {
			super();
			this.transportReceiver = transportReceiver;
		}

		@Override
		public void onTransportFailure(ServerFailure failure) {
			doOnTransportFailure(failure);
			this.transportReceiver.onTransportFailure(failure);
		}

		@Override
		public void onTransportSuccess(String payload) {
			this.transportReceiver.onTransportSuccess(payload);

		}

	}

	public GrifoRequestTransport(
			IGrifoTransportInterceptor grifoTransportInterceptor) {
		super();
		this.grifoTransportInterceptor = grifoTransportInterceptor;
	}

	@Override
	public void send(String payload, TransportReceiver receiver) {
		GrifoTransportReceiver grifoTransportReceiver = new GrifoTransportReceiver(
				receiver);
		super.send(payload, grifoTransportReceiver);
	}

	private void doOnTransportFailure(ServerFailure failure) {
		grifoTransportInterceptor.onServerFailure(failure);
	}

}
